// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Globalization;

namespace SnapObjects.Data
{
    internal class JValueReader : IValueReader
    {
        public JValueReader(string name, object value)
        {
            this.Name = name;
            this.Value = value;
        }

        public string Name { get; }
        public object Value { get; }

        public override string ToString()
        {
            return this.Value == null ? null : Convert.ToString(this.Value, CultureInfo.InvariantCulture);
        }

        public int? ToInt32()
        {
            if (this.Value == null)
            {
                return null;
            }

            return Convert.ToInt32(this.Value, CultureInfo.InvariantCulture);
        }

        public bool? ToBoolean()
        {
            if (this.Value == null)
            {
                return null;
            }

            return Convert.ToBoolean(this.Value, CultureInfo.InvariantCulture);
        }

        public double? ToDouble()
        {
            if (this.Value == null)
            {
                return null;
            }

            return Convert.ToDouble(this.Value, CultureInfo.InvariantCulture);
        }

        public decimal? ToDecimal()
        {
            if (this.Value == null)
            {
                return null;
            }

            return Convert.ToDecimal(this.Value, CultureInfo.InvariantCulture);
        }

        public DateTime? ToDateTime()
        {
            if (this.Value == null)
            {
                return null;
            }

            return Convert.ToDateTime(this.Value, CultureInfo.InvariantCulture);
        }
    }
}
